package com.atom.module.mvi.android.view

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.atom.module.mvi.binder.Binder
import io.reactivex.rxjava3.core.ObservableSource
import io.reactivex.rxjava3.core.Observer
import io.reactivex.rxjava3.functions.Consumer
import io.reactivex.rxjava3.subjects.PublishSubject

abstract class MVIActivity<Event, ViewModel> : AppCompatActivity(), ObservableSource<Event>,
    Consumer<ViewModel> {

    private val source = PublishSubject.create<Event>()
    private lateinit var binder: Binder

    abstract fun binder(): Binder

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binder = binder()
    }

    protected fun onNext(t: Event) {
        source.onNext(t)
    }

    override fun subscribe(observer: Observer<in Event>) {
        source.subscribe(observer)
    }

}